﻿using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Xml.Linq;
using System.Text;

namespace DVK.ConstantsGenerator
{
    public static class FieldsGenerator
    {
        public const string AttributeName = "Name";
        public const string AttributeID = "ID";
        public const string AttributeType = "Type";
        public const string AttributeList = "List";
        public const string ElementField = "Field";

        public static XElement _siteColumnsXml;

        public static void Generate()
        {
            string path = ConfigurationManager.AppSettings[Constants.FieldsConstantsOutput];

            //@"C:\Users\melborp\Downloads\DomainConstants.cs";
            FileStream file = File.Create(path, 1024, FileOptions.WriteThrough);
            StreamWriter writer = new StreamWriter(file);

            writer.WriteLine("using System;");
            writer.WriteLine("using System.Collections.Generic;");
            writer.WriteLine(GeneratorHelper.GetNamespaceString(Constants.Namespace));
            writer.WriteLine(GeneratorHelper.GetSummary("Generated class: " + GeneratorHelper.ParseName("Fields")));
            writer.WriteLine(GeneratorHelper.GetClassString("Fields"));

            AddStandardFields(writer);

            foreach (var field in GetSiteColumnsXml().Elements(GeneratorHelper.GetName(ElementField)))
            {
                WriteField(
                    writer,
                    field.Attribute(AttributeName).Value,
                    field.Attribute(AttributeID).Value,
                    field.Attribute(AttributeType).Value,
                    (field.Attribute(AttributeList) == null ? null : field.Attribute(AttributeList).Value),
                    field
                    );

            }
            writer.WriteLine(GeneratorHelper.GetEndString());
            writer.WriteLine(GeneratorHelper.GetEndString());
            writer.Flush();
            writer.Close();
        }

        private static void AddStandardFields(StreamWriter writer)
        {
            WriteField(writer, "Title", "{fa564e0f-0c70-4ab9-b863-0177e6ddd247}");
            WriteField(writer, "Author", "{3B4F37FC-73B6-45f8-AD63-D3ACAFC763CC}");
            WriteField(writer, "Comments", "{52578FC3-1F01-4f4d-B016-94CCBCF428CF}");
            WriteField(writer, "DateCreated", "{9f8b4ee0-84b7-42c6-a094-5cbde2115eb9}");    

            WriteField(writer, "Email", "{fce16b4c-fe53-4793-aaab-b4892e736d15}");    
            WriteField(writer, "FirstName", "{4a722dd4-d406-4356-93f9-2550b8f50dd0}");    
            WriteField(writer, "JobTitle", "{c4e0f350-52cc-4ede-904c-dd71a3d11f7d}");    
            WriteField(writer, "Department", "{05fdf852-4b64-4096-9b2b-d2a62a86bc59}");    
            WriteField(writer, "WorkPhone", "{fd630629-c165-4513-b43c-fdb16b86a14d}");    
        }

        public static void WriteField(StreamWriter writer, string name, string id)
        {
            WriteField(writer, name, id, null, null, null);
        }
        public static void WriteField(StreamWriter writer, string name, string id, string type, string list, XElement currentElement)
        {
            writer.WriteLine(
                GeneratorHelper.GetClassString(/*ElementField + */GeneratorHelper.ParseName(name))
            );

            if (!string.IsNullOrEmpty(id))
                writer.WriteLine(
                        GeneratorHelper.GetConstantFieldString(AttributeID, id)
                    );
            if (!string.IsNullOrEmpty(name))
                writer.WriteLine(
                    GeneratorHelper.GetConstantFieldString(AttributeName, name)
                );
            if (!string.IsNullOrEmpty(type))
                writer.WriteLine(
                    GeneratorHelper.GetConstantFieldString(AttributeType, type)
                );

            if (!string.IsNullOrEmpty(list))
            {
                writer.WriteLine(
                    GeneratorHelper.GetConstantFieldString(AttributeList, list)
                );
            }

            //If is choice, create enum.
            if (!string.IsNullOrEmpty(type) && type.ToLower() == "choice" && currentElement != null)
            {
                writer.WriteLine(GeneratorHelper.GetEnumString(name + "Choices"));
                
                var choices = currentElement.Descendants(GeneratorHelper.GetName("CHOICE"));
                string choicesString = string.Empty;
                foreach (XElement choice in choices)
                {
                    choicesString += "\t" + choice.Value + ",\n";
                }
                choicesString = choicesString.Remove(choicesString.LastIndexOf(','), 1);
                writer.WriteLine(choicesString);
                writer.WriteLine("\t" + GeneratorHelper.GetEndString());
            }

            writer.WriteLine("\t" + GeneratorHelper.GetEndString());
        }

        public static XElement GetSiteColumnsXml()
        {
            if (_siteColumnsXml != null)
                return _siteColumnsXml;

            XElement siteColumnsXml = XElement.Load(
                    GeneratorHelper.GetPathFromAppSettings(Constants.SiteColumnsFilePath)
                );

            _siteColumnsXml = siteColumnsXml;
            return _siteColumnsXml;
        }
    }
}